//
//  WBNaviViewController.swift
//  WeiboSwift
//
//  Created by hfwd on 2017/5/9.
//  Copyright © 2017年 hfwd. All rights reserved.
//

import UIKit

class WBNaviViewController: UINavigationController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //隐藏原来的导航栏
        navigationBar.isHidden = true
    }
    
    
    /// 重写push方法
    ///
    /// - Parameters:
    ///   - viewController: push的下一个控制器
    ///   - animated: 是否有动画效果
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if childViewControllers.count > 0 {
            
            //处理tabbar的隐藏
            viewController.hidesBottomBarWhenPushed = true
            
            //处理返回按钮
            if let vc = viewController as? WBBaseViewController {
                
                var title = "返回"
                
                //如果是根视图控制器就设置返回按钮的标题为控制器的标题，否则默认为返回
                if childViewControllers.count == 1 {
                    
                    title = childViewControllers.first?.title ?? "返回"
                }
                
                vc.naviItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(back))
            }
        }
        
        super.pushViewController(viewController, animated: true)
    }

    
    /// 返回上一级控制器
    @objc private func back(sender : AnyObject) {
        
        popViewController(animated: true)
    }

}
